/*
 * Copyright 2019 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "gd32f4xx.h"
#include "systick.h"
#include <stdio.h>
#include "main.h"
#include "gd32f450i_eval.h"
#include "touch_xpt2046.h"
#include "my_sys.h"



void SPI4_config()
{
    spi_parameter_struct  spi_init_struct;

    rcu_periph_clock_enable(RCU_GPIOF);
    rcu_periph_clock_enable(RCU_SPI4);

    /*k SPI1 GPIO config */
    gpio_af_set(GPIOF, GPIO_AF_5, GPIO_PIN_7 | GPIO_PIN_8 |GPIO_PIN_9);
    gpio_mode_set(GPIOF, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_7 | GPIO_PIN_8 |GPIO_PIN_9);
    gpio_output_options_set(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7 | GPIO_PIN_8 |GPIO_PIN_9);
    /* set SPI4_NSS as GPIO*/
    gpio_mode_set(GPIOF,GPIO_MODE_OUTPUT,GPIO_PUPD_NONE,GPIO_PIN_6);
    gpio_output_options_set(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
    gpio_bit_set(GPIOF, GPIO_PIN_6);

    /* SPI1 parameter config */
    spi_init_struct.trans_mode           = SPI_TRANSMODE_FULLDUPLEX;
    spi_init_struct.device_mode          = SPI_MASTER;
    spi_init_struct.frame_size           = SPI_FRAMESIZE_8BIT;
    spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
    spi_init_struct.nss                  = SPI_NSS_SOFT;
    spi_init_struct.prescale             = SPI_PSC_256;
    spi_init_struct.endian               = SPI_ENDIAN_MSB;
    spi_init(SPI4, &spi_init_struct);
    
    // spi_enable(SPI4);
    /* SPI int enable */
    // spi_i2s_interrupt_enable(SPI1, SPI_I2S_INT_TBE);
    // spi_i2s_interrupt_enable(SPI3, SPI_I2S_INT_RBNE);
}

void EXIT3_TPInit()
{
    /* enable and set key EXTI interrupt to the lowest priority */
    nvic_irq_enable(EXTI3_IRQn, 2U, 0U);
    /* connect key EXTI line to key GPIO pin */
    syscfg_exti_line_config(EXTI_SOURCE_GPIOI, EXTI_SOURCE_PIN3);
    /* configure key EXTI line */
    exti_init(EXTI_3, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
    exti_interrupt_flag_clear(EXTI_3);
}

void TP_Init()
{
    SPI4_config();
    spi_enable(SPI4);
    
    /* enable the key clock */
    rcu_periph_clock_enable(RCU_GPIOI);
    // rcu_periph_clock_enable(RCU_SYSCFG);
    /* configure button pin as input */
    gpio_mode_set(GPIOI, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO_PIN_3);

    // EXIT3_TPInit();
}

void ReadTPTest()
{
    char t_char[30]="";
    uint16_t t_x=0,t_y=0;
    
    gpio_bit_reset(GPIOF, GPIO_PIN_6);
    //X
    while (RESET == spi_i2s_flag_get(SPI3, SPI_FLAG_TBE));
    spi_i2s_data_transmit(SPI4, 0xD0);
    while (RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_RBNE));
    spi_i2s_data_receive(SPI4);

	delay_us(10);
    //read 1
    while (RESET == spi_i2s_flag_get(SPI3, SPI_FLAG_TBE));
    spi_i2s_data_transmit(SPI4, 0x00);
    while (RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_RBNE));
    t_x = spi_i2s_data_receive(SPI4);
    t_x = (t_x& 0x7F)<<8 ;
    //read2
    while (RESET == spi_i2s_flag_get(SPI3, SPI_FLAG_TBE));
    spi_i2s_data_transmit(SPI4, 0x90);
    while (RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_RBNE));
    t_x = t_x|spi_i2s_data_receive(SPI4);
    t_x = t_x>>3;

	delay_us(10);
    
    while (RESET == spi_i2s_flag_get(SPI3, SPI_FLAG_TBE));
    spi_i2s_data_transmit(SPI4, 0x00);
    while (RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_RBNE));
    t_y = spi_i2s_data_receive(SPI4);
    t_y = (t_y& 0x7F) <<8;

    while (RESET == spi_i2s_flag_get(SPI3, SPI_FLAG_TBE));
    spi_i2s_data_transmit(SPI4, 0x00);
    while (RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_RBNE));
    t_y = t_y|spi_i2s_data_receive(SPI4);
    t_y = t_y>>3;

    // usart_data_transmit(USART0, t_char[0]);
    // while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
    // usart_data_transmit(USART0, t_char[1]);
    // while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));

    gpio_bit_set(GPIOF, GPIO_PIN_6);

    sprintf(t_char, "x:%d\ty:%d\r\n", t_x, t_y);
    PRINTF_UART0(t_char);
}


void ReadTPXY(uint16_t *t_x, uint16_t *t_y)
{
    char t_char[30] = "";
    gpio_bit_reset(GPIOF, GPIO_PIN_6);
    //X
    while (RESET == spi_i2s_flag_get(SPI3, SPI_FLAG_TBE));
    spi_i2s_data_transmit(SPI4, 0xD0);
    while (RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_RBNE));
    spi_i2s_data_receive(SPI4);

	// delay_us(100);
    //read 1
    while (RESET == spi_i2s_flag_get(SPI3, SPI_FLAG_TBE));
    spi_i2s_data_transmit(SPI4, 0x00);
    while (RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_RBNE));
    *t_x = spi_i2s_data_receive(SPI4);
    *t_x = (*t_x& 0x7F)<<8 ;
    //read2
    while (RESET == spi_i2s_flag_get(SPI3, SPI_FLAG_TBE));
    spi_i2s_data_transmit(SPI4, 0x90);
    while (RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_RBNE));
    *t_x = *t_x|spi_i2s_data_receive(SPI4);

    // sprintf(t_char, "x:%x\t", *t_x);
    // PRINTF_UART0(t_char);

    *t_x = *t_x>>3;

	// delay_us(100);
    
    while (RESET == spi_i2s_flag_get(SPI3, SPI_FLAG_TBE));
    spi_i2s_data_transmit(SPI4, 0x00);
    while (RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_RBNE));
    *t_y = spi_i2s_data_receive(SPI4);
    *t_y = (*t_y& 0x7F) <<8;

    while (RESET == spi_i2s_flag_get(SPI3, SPI_FLAG_TBE));
    spi_i2s_data_transmit(SPI4, 0x00);
    while (RESET == spi_i2s_flag_get(SPI4, SPI_FLAG_RBNE));
    *t_y = *t_y|spi_i2s_data_receive(SPI4);

    // sprintf(t_char, "y:%x\t", *t_y);
    // PRINTF_UART0(t_char);

    *t_y = *t_y>>3;
}

void ReadTPXYOver(uint16_t *t_x, uint16_t *t_y)
{
    uint16_t t_xx[5], t_yy[5];
    uint16_t i = 0, j = 0, temp = 0;
    char t_char[30] = "";
    for (i = 0; i < 5; i++)
    {
        ReadTPXY(&t_xx[i], &t_yy[i]);
        // sprintf(t_char, "x:%d\ty:%d\r\n", t_xx[i], t_yy[i]);
        // PRINTF_UART0(t_char);
    }
    //冒泡排序
    //x
    for (i = 0; i < 5 - 1; i++)
        for (j = 0; j < 5 - i - 1; j++)
        {
            if (t_xx[j] > t_xx[j + 1])
            {
                temp = t_xx[j + 1];
                t_xx[j + 1] = t_xx[j];
                t_xx[j] = temp;
            }
        }
    //取中位数
    *t_x = t_xx[2];
    //y
    for (i = 0; i < 5 - 1; i++)
        for (j = 0; j < 5 - i - 1; j++)
        {
            if (t_yy[j] > t_yy[j + 1])
            {
                temp = t_yy[j + 1];
                t_yy[j + 1] = t_yy[j];
                t_yy[j] = temp;
            }
        }
    //取中位数
    *t_y = t_yy[2];
}